/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import Algorithm.*;
/**
 *
 * @author ErikLee
 */
public class BoardPanel extends JPanel {
    /**棋盘偏离窗体左上角的数值*/
    private final static int OFFSET = 25;
    /**棋盘格子的边框宽度*/
    private final static int LINE_WIDTH = 5;
    /**棋盘格子的大小*/
    private final static int CELL_WIDTH = 100;
    /**棋盘的列数*/
    private final static int NUM_COLS = 3;
    /**棋盘的行数*/
    private final static int NUM_ROWS = 3;
    /**棋盘的宽度*/
    private final static int BOARD_WIDTH =
            LINE_WIDTH + NUM_COLS * (CELL_WIDTH + LINE_WIDTH);
    /**棋盘的高度*/
    private final static int BOARD_HEIGHT =
            LINE_WIDTH + NUM_ROWS * (CELL_WIDTH + LINE_WIDTH);
    /**棋盘面板的宽度*/
    private final static int PANEL_WIDTH = BOARD_WIDTH + OFFSET * 2;
    /**棋盘面板的高度*/
    private final static int PANEL_HEIGHT = BOARD_HEIGHT + OFFSET * 2;

    /**棋盘背景，用一个填充的长方形表示棋盘*/
    private Rectangle2D.Double _background;
    /**棋盘格子矩阵，实际上是在黑色背景上填充白色小方格，留出来的黑色背景成为边框*/
    private BoardSquare[][] _square;

    private int side;

    private TicTacToe tictactoeStrategy;

    private int gameNum = 0;

    /**
     * 构造方法，初始化棋盘背景尺寸，棋盘方格尺寸
     */
    public BoardPanel() {
        _background = new Rectangle2D.Double(
                OFFSET,
                OFFSET,
                LINE_WIDTH + NUM_ROWS * (CELL_WIDTH + LINE_WIDTH),
                LINE_WIDTH + NUM_COLS * (CELL_WIDTH + LINE_WIDTH));
        _square = new BoardSquare[NUM_ROWS][NUM_COLS];

        for (int row = 0; row < NUM_ROWS; row++) {
            for (int col = 0; col < NUM_COLS; col++) {
                _square[row][col] = new BoardSquare(
                        OFFSET + LINE_WIDTH + row * (CELL_WIDTH + LINE_WIDTH),
                        OFFSET + LINE_WIDTH + col * (CELL_WIDTH + LINE_WIDTH),
                        CELL_WIDTH,
                        this);
            }
        }
        this.setPreferredSize(new Dimension((int) PANEL_WIDTH, (int) PANEL_HEIGHT));
        this.addMouseListener(new MyMouseListener());

        restBoard();

    }

    /**
     * 画棋盘
     * @param aPaintBrush 画笔
     */
    public void paintComponent(Graphics aPaintBrush) {
        super.paintComponent(aPaintBrush);
        Graphics2D pen = (Graphics2D) aPaintBrush;
        //first, draw thw background
        Color savedColor = pen.getColor();
        pen.setColor(Color.black);
        pen.fill(_background);

        //then, draw the square
        for (int row = 0; row < NUM_COLS; row++) {
            for (int col = 0; col < NUM_ROWS; col++) {
                pen.setColor(_square[row][col].getColor());
                _square[row][col].fill(pen);
            }
        }

        pen.setColor(savedColor);
    }

    public void changeSide(){
        if (side == TicTacToe.HUMAN) {
            side = TicTacToe.COMPUTER;
        } else {
            side = TicTacToe.HUMAN;
        }
    }

    public void restBoard() {
        tictactoeStrategy = new TicTacToe();
        for (int row = 0; row < NUM_COLS; row++)
            for (int col = 0; col < NUM_ROWS; col++)
                _square[row][col].restPiece();
        repaint();
        System.out.println("clear");
    }

        public void doCompMove(boolean thinkAboutIt) {
            Best compMove;

            if (thinkAboutIt) {
                compMove = tictactoeStrategy.chooseMove(TicTacToe.COMPUTER);
            } else {
                compMove = new Best(0, gameNum % 3, gameNum / 3);
                gameNum = (gameNum + 1) % 9;
            }

            System.out.println(" ROW = " + compMove.row + " COL = " + compMove.column);

            _square[compMove.row][compMove.column].putPiece(TicTacToe.COMPUTER);
           
            tictactoeStrategy.playMove(TicTacToe.COMPUTER, compMove.row, compMove.column);
            repaint();
        }

        public boolean restIfDone(boolean condition, String message, boolean compMoves) {
            if(condition) {
                JOptionPane.showMessageDialog(null, message);
                restBoard();
                if(compMoves) {
                    doCompMove(false);
                }
            } else {

            }

            return condition;
        }

    /**
     * 鼠标监听器类
     */
    public class MyMouseListener extends MouseAdapter {

        /**
         * 鼠标单击事件，判断哪一个方格被点击，并改变它的颜色
         * @param e
         */
        @Override
        public void mouseClicked(MouseEvent e) {
            boolean foundSquare = false;
            for (int row = 0; row < NUM_COLS && !foundSquare; row++)
                for (int col = 0; col < NUM_ROWS && !foundSquare; col++)
                    if (_square[row][col].contains(e.getPoint())) {
                        foundSquare = true;
                        _square[row][col].mouseClicked(e);
                        tictactoeStrategy.playMove(TicTacToe.HUMAN, row, col);
                    }
            if(restIfDone(tictactoeStrategy.boardIsFull(), "平局！", true)) {
                return;
            }

            doCompMove(true);
            restIfDone(tictactoeStrategy.isAWin(tictactoeStrategy.COMPUTER), "电脑赢了！", true);
            restIfDone(tictactoeStrategy.boardIsFull(), "平局！", false);
        }
    }
}